package org.jeecg.modules.tt_question_collect.util;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.math.RandomUtils;
import org.jeecg.common.handler.IFillRuleHandler;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionCollect;

public class TransUntil {

	/*
	 * 大写转小写list
	 */
	public static List<Map<String, Object>> transformLowerCase(List<Map<String, Object>> hlist) {
		List<Map<String, Object>> llist = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (int i = 0; i < hlist.size(); i++) {
			Map<String, Object> newMap = new HashMap<String, Object>();
			map = hlist.get(i);
			Set<String> set = map.keySet();
			for (String key : set) {
				String newKey = key.toLowerCase();
				newMap.put(newKey, map.get(key));
			}
			llist.add(newMap);
		}
		return llist;
	}

	public static JSONObject sendPost(String url, String param) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常！"+e);
			e.printStackTrace();
		}
		//使用finally块来关闭输出流、输入流
		finally{
			try{
				if(out!=null){
					out.close();
				}
				if(in!=null){
					in.close();
				}
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
		}
		JSONObject jsonObject1 =JSONObject.parseObject(result);
		return jsonObject1;

	}


	/**
	 * 生成问题编号
	 * @param ttQuestionCollect
	 * @param phone
	 * @return
	 */
	public static String wtcode(TtQuestionCollect ttQuestionCollect, String code) {


		String prefix = "";
		//订单前缀默认为CN 如果规则参数不为空，则取自定义前缀
		if (ttQuestionCollect.getQuestiontype() != null && ttQuestionCollect.getProname()!=null) {
			String	xqxg = ttQuestionCollect.getQuestiontype();

			if("SJXG".equals(ttQuestionCollect.getQuestiontype())){
				xqxg = "XG";
			}

			prefix = ttQuestionCollect.getProname()+xqxg;
		}

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		//  int random = RandomUtils.nextInt(90) + 10;
		String value = prefix + format.format(new Date());




			if (!StringUtils.isEmpty(code)) {
			value += code;
		}
		return value;
	}

	public static String baseendcode(String params){
		//String str = "Hello World";
		try{
			byte[] encodeBase64 = Base64.encodeBase64(params.getBytes("UTF-8"));
			System.out.println("encodeBase64: " +  (encodeBase64));
			System.out.println("RESULT: " + new String(encodeBase64));
			return new String(encodeBase64);
		} catch(UnsupportedEncodingException e){
			e.printStackTrace();
			return null;
		}


	}

	public static String testbaseendcode(String params){
		//String str = "Hello World";
		String[] arr = params.split(",");

		try{
			String str="";
			for (int i =0;i<arr.length;i++){
				String code ="http://work.hbidding.com/jeecg-boot/sys/common/static/"+arr[i];
			//	System.out.println("code"+new String(code));
				byte[] encodeBase64 = Base64.encodeBase64(code.getBytes("UTF-8"));
				System.out.println("encodeBase64"+encodeBase64);
				System.out.println("new String"+new String (encodeBase64));
				String test = "http://work.hbidding.com/preview/onlinePreview?url="+ new String (encodeBase64);
				str += test;
				if(i != arr.length -1){
					str += ",";
				}


			}

			System.out.println("RESULT:"+ str);
			return (str);
		} catch(UnsupportedEncodingException e){
			e.printStackTrace();
			return null;
		}
	}




}
